import formJs from "./regexDetail.js"; //新增弹框配置项
// import publicForm from "@/components/public/up-form.vue"; //弹框form---多选、照片
import publicForm from "@/components/public/el-form.vue"; //弹框form
import CustomDialog from "@/components/HntManage/pouringOrder/WBSHuitanGangbao/index.vue";
// import Vue from 'vue';
import CustomDialog1 from "@/components/fieldTestManage/reboundEntrustList/index.vue";//委托单列表页

import {
    saveFieldtestCheckTask, //保存
    getFieldtestCheckTaskByFid, //详情
    getBaseSgCompanyList, //标段名称
    getPileFoundationTaskEquipList, //设备编号
    getBaseDataitemDetailByItemCode, //设计强度等级(Qdlevel)
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname",
        "radioTypes"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            WBSShow: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            formObj: formJs.formObj1,
            formData: {
                fId: "",
                fCompanyid: "", //所属标段
                fCompanyname: "", //所属标段
                fProjectid: "", //项目id 隐藏
                fProjectname: "", //项目简称
                fEngineeringpartid: "", //工程部位id
                fEngineeringpartname: "", //工程部位
                fCheckcompany: "", //检测单位
                fEquipname: "", //设备编号
                fCreatedates: this.common.getDay1(0), //创建时间时分秒不可编辑
                fCreateuserid: "", //当前登录人id
                fCreateusername: "", //当前登录
                fChecktype: "", //检测类型
                fWitnessid: "", //委托编号id
                fWitnessno: "", //委托编号
                fNature: "", // 当前登录用户的机构性质
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名

                fSpecimenage: '', //龄期
                fConcretelevel: "", //设计强度等级(MPa)
                fConcretetype: "", //是否为泵送混凝土
                fSurface: "", //测面状态
                fAngle: "", //测试角度

                fThicknessdesignvalue: "", //设计厚度
                fThickness:"", //垫块厚度
                fCorrectionfactor: "", //修正因子
                fAllowdeviation1: "", //允许偏差(+mm)
                fAllowdeviation: "", //允许偏差(mm)

            },
            btnShow: true,
            currentDetail: null,
            fCompanynames: null,
            fCompanyids: null,
            fileList: [], //附件
            types: [],
            angleData: [
                //   {
                //   value: '-1',
                //   label: '0°'
                // },
                {
                    value: "0",
                    label: "90°",
                },
                {
                    value: "1",
                    label: "60°",
                },
                {
                    value: "2",
                    label: "45°",
                },
                {
                    value: "3",
                    label: "30°",
                },
                {
                    value: "4",
                    label: "-30°",
                },
                {
                    value: "5",
                    label: "-45°",
                },
                {
                    value: "6",
                    label: "-60°",
                },
                {
                    value: "7",
                    label: "-90°",
                },
            ], //角度数据
            // 委托单编号

            isShow: false,
            // 委托单编号

        };
    },
    components: {
        publicForm,
        CustomDialog,
        CustomDialog1
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },

        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId,
                            fChecktype: this.currentDetail.fChecktype
                        })
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal;
                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal;
                }
            },
            immediate: true,
        },
        radioTypes: {
            handler(newVal) {
                if (newVal) {
                    this.types = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        if (this.userInfo.companyNature == "中心试验室") {
            // this.getTree();
        }
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true;
        this.formData.fCreateusername = this.userInfo.realName; //委托人
        this.formData.fCreateuserid = this.userInfo.userId; //委托人
        this.formData.fProjectname = this.userInfo.projectFullName; //委托人

        this.formData.fProjectid = this.userInfo.projectId; //委托人
        if (this.types == 1) { //钢保
            this.formObj = formJs.formObj2;
            this.formData.fChecktype = "钢筋保护层检测";

            this.formObj.map((item) => {
                item.disabled = false;
            });

            this.formObj[13].disabled = true;
        } else if (this.types == 0) { //回弹
            this.formObj = formJs.formObj1;
            this.formData.fChecktype = "回弹检测";

            this.formObj.map((item) => {
                item.disabled = false;
            });

            this.formObj[12].disabled = true;
            this.formObj[8].disabled = false;
            this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)

        } else { //其他 成孔桩基
            this.formObj = formJs.formObj;
            if (this.types == 2) {
                this.formData.fChecktype = "桩基检测";
            }
            else if (this.types == 3) {
                this.formData.fChecktype = "成孔质量检测";
            }
        }


        if (this.statusNum == 1) { //新增
            if (this.userInfo.companyNature == "中心试验室") {
                this.formObj[2].disabled = false;
                this.formObj[1].disabled = false;
                this.getBaseTree(0);
            } else {
                this.formData.fCompanyname = this.userInfo.companyFullName;
                this.formData.fCompanyid = this.userInfo.companyId;
                this.EquipList(this.formData.fChecktype, 0)
                this.formObj[2].disabled = true;
                this.formObj[1].disabled = true;
            }
            this.formObj[0].disabled = true;

            this.formObj[4].disabled = true;
            this.formObj[5].disabled = true;


        } else if (this.statusNum == 2) { //编辑
            if (this.currentDetail.fChecktype == '钢筋保护层检测') {
                this.formObj = formJs.formObj2;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[13].disabled = true;
            } else if (this.currentDetail.fChecktype == '回弹检测') {
                this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
                this.formObj = formJs.formObj1;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[2].disabled = true;
                this.formObj[12].disabled = true;
            } else if (this.currentDetail.fChecktype == "桩基检测") {
                this.formObj = formJs.formObj;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[7].disabled = true;
                this.formObj[8].disabled = false;
            } else if (this.currentDetail.fChecktype == "成孔质量检测") {
                this.formObj = formJs.formObj;
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[7].disabled = true;
                this.formObj[8].disabled = false;
            }
            this.formObj[0].disabled = true;
            this.formObj[4].disabled = true;

            // else if(this.currentDetail.fChecktype=="成孔质量检测"){
            //     this.formObj=formJs.formObj;
            //     this.formObj[7].disabled = true;
            //     this.formObj[8].disabled = false;


            // }

            if (this.userInfo.companyNature == "中心试验室") {
                this.getBaseTree(1);
                this.formObj[2].disabled = false;
                this.formObj[5].disabled = true;
                this.formObj[1].disabled = false;
            } else {

                this.formData.fCompanyname = this.userInfo.companyFullName;
                this.formData.fCompanyid = this.userInfo.companyId;

                this.formObj[2].disabled = true;
                this.formObj[5].disabled = true;

                this.formObj[1].disabled = true;
            }
            this.formData.fId = this.currentDetail.fId //按钮 
            // this.formObj[5].disabled = true;
            // this.formObj[4].disabled = true;
            // this.formObj[8].disabled = true;
            // this.formObj[6].inputButton = true;

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false;
            if (this.currentDetail.fChecktype == '钢筋保护层检测') {
                this.formObj = formJs.formObj2;

            } else if (this.currentDetail.fChecktype == '回弹检测') {
                this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
                this.formObj = formJs.formObj1;

            } else if (this.currentDetail.fChecktype == "桩基检测") {
                this.formObj = formJs.formObj;

            } else if (this.currentDetail.fChecktype == "成孔质量检测") {
                this.formObj = formJs.formObj;

            }
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[6].inputButton = false;
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        getEntrustRow(data) {
            this.formData.fWitnessno = data.fWtnumber;
            this.formData.fWitnessid = data.fId;
            this.formData.fCompanyid = data.fCompanyid;//施工单位id
            this.formData.fCompanyname = data.fCompanyname;//施工单位名称
            this.formData.fEngineeringpartid = data.fId;//工程部位id
            this.formData.fEngineeringpartname = data.fEngineeringpartname;//工程部位
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
            if (b == "fCompanyname") {
                this.formData.fCompanyname = data.label;
                this.formData.fCompanyid = data.value;

                this.EquipList(this.formData.fChecktype, 1);
                this.formData.fEquipname = ""; //设备编号
                this.formData.fCheckcompany = '';

            }
            if (a == "fWitnessno") {
                this.isShow = true;
            }
            else if (b == "fChecktype") {
                this.formData.fEquipname = ""; //设备编号
                this.formData.fCheckcompany = '';
            } else if (b == 'fEquipname') {
                this.formData.fEquipname = data.label;
                this.formData.fCheckcompany = data.value;
            } else if (b == 'fSurface') {
                if (data == '侧面') {
                    this.formData.fAngle = "0°"
                    this.formObj[11].options = [{
                        label: "0°",
                        value: "0°"
                    }]
                } else if (data == '表面') {
                    this.formData.fSurface = "表面";
                    this.formData.fAngle = "90°"
                    this.formObj[11].options = this.angleData
                } else if (data == '底面') {
                    this.formData.fAngle = "90°"
                    this.formObj[11].options = this.angleData
                }
            } else if (b == 'fAngle') {
                this.formData.fAngle = data;
            } else if (b == 'fWitnessno') {
                this.formData.fWitnessid = data.value; //委托编号id
                this.formData.fWitnessno = data.label; //委托编号
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        refreshList() { },
        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        clickDialog(index, prop) {

            // 取样--工程部位
            if (prop == "fEngineeringpartname") {
                this.WBSShow = true;
            }

            return index, prop;
        },
        // 获取列表数据 设备编号
        // 获取委托单列表数据
        closeDialog1() {
            this.isShow = false;
        },
        EquipList(data, num) {
            var obj = {
                fCompanyid: this.formData.fCompanyid, //左侧选中组织机构id
                page: 1, //当前页码
                pageSize: 500, //每页显示条数
                keyType: data
            }
            this.loading = true;
            var that = this;
            this.formObj[3].options = [];
            getPileFoundationTaskEquipList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        res.data.equipList.list.map(item => {
                            this.formObj[3].options.push({
                                label: item.fCheckequipname,
                                value: item.fCheckcompanyname
                            })
                            if (num == 0) {
                                this.formData.fCheckcompany = res.data.equipList.list[0].fCheckcompanyname;
                                this.formData.fEquipname = res.data.equipList.list[0].fCheckequipname;

                            }
                        })
                        // 
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })

        },
        getWBS(data) { //获取选择WBS信息
            this.formData.fEngineeringpartname = data.fLongname //工程部位
            this.formData.fEngineeringpartid = data.fNodeid //工程部位id
        },
        // 标段名称
        getBaseTree(num) {
            this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
            var data = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId
            }

            this.formObj[2].options = [];
            getBaseSgCompanyList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.userCompanyTreeList;
                        data.map(item => {
                            this.formObj[2].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })
                        if (data.length != 0) {
                            this.formData.fCompanyname = this.formObj[2].options[0].label;
                            this.formData.fCompanyid = this.formObj[2].options[0].value;

                            this.EquipList(this.formData.fChecktype, num);
                        }
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });

        },


        // 数据字典
        getBaseDataitemDetailByItemCode(status) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {
                this.formObj[8].options = [];
                var data = res.data.baseDataitemdetailList
                var arr = []
                data.map(item => {
                    arr.push({
                        value: item.fItemdetailid,
                        label: item.fItemvalue
                    })
                })
                this.formObj[8].options = arr
            })
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getFieldtestCheckTaskByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.pileTask;
                    this.formData = data;
                    if (data.fChecktype == '回弹检测') {
                        if (data.fSurface == '侧面') {
                            this.formObj[11].options = [{
                                label: "0°",
                                value: "0°"
                            }]
                        } else if (data.fSurface == '表面') {
                            this.formObj[11].options = this.angleData
                        } else if (data.fSurface == '底面') {
                            this.formObj[11].options = this.angleData
                        }
                    }
                    this.EquipList(this.currentDetail.fChecktype, 1)

                    // if (this.statusNum == 1) {
                    //     this.formData.recordfiles = [];
                    //     console.log(this.userInfo.userId);
                    //     // 委托人
                    //     if (data.fCommissionid == null || data.fCommissionid == "" || data.fCommissionname == null || data.fCommissionname == "") {
                    //         this.formData.fCommissionid = this.userInfo.userId;
                    //         this.formData.fCommissionname = this.userInfo.realName;
                    //     }
                    //     // 委托日期
                    //     if (data.fCommissiondates == null || data.fCommissiondates == "") {
                    //         this.formData.fCommissiondates = this.common.day(0)
                    //     }
                    // }


                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }

            });
        },
        yj_saveForm() {
            this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fNature = this.userInfo.companyNature;
            console.log(this.formData);
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(obj) {
            // var data = {
            //     fId: obj.fId, //必传
            //     userId: obj.userId, //当前登录人id
            //     userName: obj.userName, //当前登录人姓名
            //     recordfiles: obj.recordfiles, //删除文件id集合
            //     fCommissiondates: obj.fCommissiondates, //委托日期
            //     fCommissionname: obj.fCommissionname, //委托人
            //     fCommissionid: obj.fCommissionid, //委托人id
            //     fUnitname: obj.fUnitname, //接收委托单位名称
            //     fUnitid: obj.fUnitid, //接收委托单位id
            //     fOrdernumber: obj.fOrdernumber, //委托单号
            //     fFileurl: obj.fFileurl, //委托单附件
            //     fNote: obj.fNote, //备注
            //     fCompanyid: obj.fCompanyid,
            //     fChecktype: obj.fChecktype,
            // }
            this.loading = true;
            this.isLock = true
            saveFieldtestCheckTask(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = true;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = true;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })

        },


    },
};